
/*
	author: S. M. Shahriar Nirjon
	last modified: August 30, 2008
*/
# include "iGraphics.h"
#include < math.h >
#include <string.h>

// Declairetion of prototypes

void netedRectangle( int mx , int my , int differy , int differx   );
void draw (void);
void goalKdraw (void);
void identiFication (void);
void teamInitialise (void);
void moveUp ( int *x , int *y );
void moveDown ( int *x , int *y );
void moveLeft( int *x , int *y );
void moveRight( int *x ,int *y );
void RunningTeamUp ( int *p , int *q ,int minimum );
void RunningTeamDown ( int *p , int *q ,int minimum );
void ControlChange( int *a ,int *b ,int *c );
void teamDraw( int *p , int *q );
int DistanceCount( int *x ,int *y );
void playerDistance(void);
void drawfield(void);
void goalKdraw(void);
void pass(int x, int y , int x1 , int y1);
void Result(void);

// Taking global variables
int  x, mx=0 , my =0 ;
double ballx = 100 , bally = 120 ,rad = 5 , Soron , PassDist , DifferX , DifferY , dy ,dx ; // About ball
int  cenDist  , y , pldist  , randret , f ,initial , timerCount = 1 ,index1 , index2 ;

int playerCoordX[6] = { 205 , 409 , 303 , 214 , 403 , 301 }; 
int playerCoordY[6] = { 315 , 317 , 249 , 167 , 179 , 214 };
int playerRangeUP[6] = { 577 , 571 , 449 , 314 , 279 , 314 };
int playerRangeDOWN[6] = { 215 , 217 , 149 ,114 , 79 , 114 };
int oponentCoordX[6] = { 164 , 459 , 298 , 296 , 433 , 192  };
int oponentCoordY[6] = {524 ,  521  ,  554 , 447, 392 , 403 } ;
int oponentRangeUp[6] = { 590, 560 , 580 , 500, 480, 460};
int oponentRangeDown[6] = {320 , 330 , 370 ,230, 150 ,130 };

int  min  , max  , min2 , distance , index , d = 1 , move = 1 , ball , passingcon , windowX = 1000 , windowY = 700 , speed  , passTimer = 1, KickTake = 2 ;
int controlconstant = 1 , i;
int minInd = 1000000000 , minInd2 = 10000000000 , goalKx = 295 , goalKy = 60 , goalKy1 = 640  ; // For two goal keeper
char PlayerName[6][20]={
	"Messi" ,
    "Tevej" ,
	"Riquelme" ,
	"Sorin" ,
	"Aimer" ,
	"Aguero" 
};

void identiFication(void)
{
	iSetColor(0 , 0 , 255 );
	iText( 680 , 550 , " Football",GLUT_BITMAP_TIMES_ROMAN_24);
	iSetColor(255 , 0 , 0  );
	iText( 700,500,"1970",GLUT_BITMAP_HELVETICA_18);
}

void teamInitialise(void)
{
	playerCoordX[0] = 205 ;
	playerCoordX[1] = 409 ;
    playerCoordX[2] = 303 ;
	playerCoordX[3] = 214 ;
	playerCoordX[4] = 403 ;
	playerCoordX[5] = 301 ;

	playerCoordY[0] = 315 ;
    playerCoordY[1] = 317 ;
    playerCoordY[2] = 249 ;
	playerCoordY[3] = 167 ;
    playerCoordY[4] = 179 ;
	playerCoordY[5] = 214 ;

	ballx=100;
	bally=120;
	
	goalKy =60;
	goalKy1=640;
	goalKx = 295;
}

void moveUp ( int *x , int *y )
{
    if( *y < 640 || bally < 650 )
	{
		distance = int (pow( *x - ballx,2) + pow( *y - bally,2));
			
		if( distance < 225)
		{
            if(bally != *y + 15 )bally = bally+1; 
			
    		if(ballx == *x) 
			{
				if( bally < *y ) 
				{
					bally = *y ;
					bally = bally+15;
				}
				else ;
			}
			
			else if(ballx < *x) ballx = ballx++  ;
			else
			{
					
				 ballx = ballx-1;
				bally = bally+1;
			}
		        
			(*y)++;
			bally++;
				
		} 
    	else if( *y < 640 ) (*y)++ ;
	}
}


void moveDown( int *x , int *y )
{
		if(*y > 60 || bally > 50 )
		{
			distance = int ( pow( *x - ballx , 2 ) + pow( *y - bally , 2 ) );
			
			if( distance < 225)
			{
				if( ballx == *x ) 
				{
					if( bally > *y)
					{
						bally =  *y ;
						bally =  bally - 15;
					}
					else;
				}

 				else
				{
					if(ballx > *x)
					{
				      
					   ballx = ballx -1  ;
				       bally = bally-1 ;
					}
					else
					{
					    
					    ballx = ballx+1;
						bally = bally-1; 
					}
				}
				bally-- ;
				(*y) --;
				
			}
			else if( *y > 60) (*y)-- ;
		}
}
void moveLeft( int *x , int *y )
{
    if( *x > 90 || ballx > 80) 
	{
		distance = int ( pow ( *x - ballx , 2 ) + pow ( *y - bally , 2 ) );
			
		if( distance < 225)
		{
			if( bally == *y ) 
			{
				if( ballx > *x ) 
				{
					ballx = *x ;
					ballx = ballx - 15;
				}
				else ;
			}

			else
			{
				if( bally > *y  )
				{
					bally = bally - 1 ;
					ballx = ballx - 1 ;
				}
				else
				{
		        	ballx = ballx - 1 ;
			        bally = bally + 1 ;
				}
			}
			ballx-- ;
		    (*x)--;
				
		}
		else if( *x > 90 ) (*x)-- ;
	}
}

void moveRight( int *x , int *y)
{
		if( *x < 520 || ballx < 520 )  
		{
			distance = int ( pow(*x - ballx , 2 ) + pow ( *y - bally , 2 ) );
			
			if( distance < 225)
			{
				if( bally == *y ) 
				{
					if( ballx < *x )
					{
						ballx = *x ;
						ballx = ballx + 15 ;
					}
					else ;
				}

				else
				{
					if( bally > *y )
					{
				         ballx = ballx + 1 ;
				         bally = bally - 1 ;
					}
					else
					{
						 ballx = ballx + 1 ;
						 bally = bally + 1 ;
					}

				}
				ballx++ ;
				(*x)++;
				
				
			}
			else if( *x < 520 ) (*x)++ ;
		}
}
void UpRightMove( int *x , int *y )
{
  if( *x <520 && *y < 640 )
		{
			distance = int ( pow ( *x - ballx , 2 )  + pow ( *y - bally , 2 ) );
			if( distance < 225)
			{
				if(ballx != *x + 10 ) ballx = *x + 9 ;
				if(bally != *y + 10 ) bally = *y + 9 ;
				(*x)++ ;
				(*y)++ ;
				ballx++;
				bally++;
			}
			else
			{
				(*x)++;
				(*y)++;
			}
			
		}
}
void UpLeftMove( int *x , int *y)
{
	if( *x >90 && *y < 640 )
		{
			distance = int ( pow ( *x - ballx , 2 ) + pow ( *y - bally , 2 ) );
			if( distance < 225)
			{
				if(ballx != *x - 10 ) ballx = *x - 9;
				if(bally != *y + 10 ) bally = *y + 9 ;
				(*x)-- ;
				(*y)++ ;
				ballx--;
				bally++;
			}
			else
			{
				(*x)--;
				(*y)++;
			}
		}

}

void DownLeftMove( int *x , int *y )
{
	if( *x > 90 && *y > 60 )
		{
			distance = int ( pow (*x - ballx , 2 ) + pow ( *y - bally , 2 ) );			
			if( distance < 225)
			{   
				if(ballx != *x  - 10 ) ballx = *x  - 9 ;
				if(bally != *y - 10 ) bally = *y - 9 ;
				(*x)-- ;
				(*y)-- ;
				ballx--;
				bally--;
			}
			else
			{
				(*x)--;
				(*y)--;
			}
		}
}
void DownRightMove( int *x , int *y )
{
        if( *x < 520 && *y > 60 )
		{
			distance = (*x - ballx )*( *x - ballx )  + ( *y - bally ) * ( *y - bally );
			if( distance < 225)
			{
				
				if(ballx != *x + 10 ) ballx = *x + 9 ;
				if(bally != *y - 10 ) bally = *y - 9 ; 
				
				ballx++;
				bally--;
				(*x)++ ;
				(*y)-- ;
			}
			else
			{
				(*x)++;
				(*y)--;
			}
		}
}
	// for moving all players of the team random speed
void RunningTeamUp( int *p , int *q ,int minimum )
{
	for ( int  i = 0 ; i < 6 ; i++ )
	{	
		if ( i == minimum  ) ;
		else
		{
			if(	*p < *q)
			{
				if( rand() % 2 == 0); //playerCoordY[index]++;
				else (*p)++;
			}
		}
		p ++;
	    q++ ;
	}

}

void RunningTeamDown( int *p , int *q ,int minimum )
{
	for ( int  i = 0 ; i < 6 ; i++ )
	{	
		
		if ( i == minimum  ) ;
		else
		{
			if(	*p > *q)
			{
				if( rand() % 2 == 0); //playerCoordY[index]++;
				else (*p)--;
				
			}
		}
		p ++;
	    q++ ;
	}

}

void playerDistance()
{
	for ( index = 0 ; index < 6 ; index++ )
	{
		if ( index == min );
		else 
		{
           pldist = pow(playerCoordX[min] - playerCoordX[index] ,2 ) + pow( playerCoordY[min] - playerCoordY[index] ,2);
		   
		   if ( pldist < minInd2 ) 
		   {
			   min2 = index ;
			   minInd2 = pldist ;
		   }
		}
	}
}


//draw a team
void teamDraw( int *p , int *q)
{
	
	for ( index = 0 ; index < 6 ; index++ )
	{
		iFilledCircle( *p , *q , 10 );
		p++;
		q++;
	}
}
 
// Find the player nearest to the ball
int  DistanceCount( int *x ,int *y)
{
	int DistanceP ,minimum;
		int minInd = 10000000 ;
	 for ( index = 0 ; index < 6 ; index++ )
	{
		
		 DistanceP = int( pow ( *x - ballx , 2 ) + pow ( *y - bally , 2 ));
		if( DistanceP < minInd ) 
		{
			minimum = index ;
			minInd = DistanceP ;
		}
		x++ ;
		y++ ;
	 }
     return minimum ;
	 
 }
void BallInTouch(void)
{
	distance = int( pow ( playerCoordX[min] - ballx , 2) + pow (playerCoordY[min] - bally , 2));
}

void Kick( )
{
	
           if( initial < 90 ) 
			{
				if( DifferX != 0 )
				{
					if(ballx > playerCoordX[min] )ballx = ballx + dx ;
					else if( ballx < playerCoordX[min]) ballx = ballx - dx ;
				}
				else ;
				if( DifferY != 0 )
				{
					if( bally > playerCoordY[min])bally = bally + dy ;
					else if( bally < playerCoordY[min])bally = bally - dy ;
				}
				else ;
				rad = rad + .05 ;
			}
			else if ( initial < 189 && rad >= 5)
			{   
	             if( DifferX != 0 )
				{
					if(ballx > playerCoordX[min] )ballx = ballx + dx ;
					else if( ballx < playerCoordX[min]) ballx = ballx - dx ;
				}
				else ;
				if( DifferY != 0 )
				{
					if( bally > playerCoordY[min])bally = bally + dy ;
					else if( bally < playerCoordY[min])bally = bally - dy ;
				}
				else ;	
				 rad = rad - .05 ;
			}
			else 
			{
				if( DifferX != 0 )
				{
					if(ballx > playerCoordX[min] )ballx = ballx + dx ;
					else if( ballx < playerCoordX[min]) ballx = ballx - dx ;
				}
				else ;
				if( DifferY != 0 )
				{
					if( bally > playerCoordY[min])bally = bally + dy ;
					else if( bally < playerCoordY[min])bally = bally - dy ;
				}
				else ;
			}
			initial++ ;
			if( initial==200)
			{
				iPauseTimer(index1);
				timerCount = 2 ;
				initial = 0 ;
				 min= DistanceCount(playerCoordX ,playerCoordY);
				 KickTake = 2 ;
			}
}
void passPlayer(void)
{
	
	if( PassDist < 150 ) 
	{
				if( DifferX != 0 )
				{
					if(ballx > playerCoordX[min] )ballx = ballx + dx ;
					else if( ballx < playerCoordX[min]) ballx = ballx - dx ;
				}
				else ;
				if( DifferY != 0 )
				{
					if( bally > playerCoordY[min])bally = bally + dy ;
					else if( bally < playerCoordY[min])bally = bally - dy ;
				}
				else ;
	    			
	}
    PassDist++ ;
	if( PassDist == 150 )
	{
		iPauseTimer(index2);
        passTimer = 2 ;	   
		PassDist = 0 ;
		min= DistanceCount(playerCoordX ,playerCoordY);
		
	}
	

				
			
}

void goalKdraw(void )// drawing for goal keeper
{
	iSetColor(131,131,243);
	
	iFilledCircle( goalKx , goalKy  , 10);
	iFilledCircle( goalKx , goalKy1 , 10 );
	
}

void Result(void)//prints Goal in the screen
{
	if(bally >650 && ballx >260 && ballx <350 )
	{
	    f = 1 ;
		iSetColor(217 ,27 ,222);
		iText(680 ,350 ,"GOAL!!!!",GLUT_BITMAP_TIMES_ROMAN_24);
		if(f == 1)
		{
			iSetColor(73,49,210);
			iFilledRectangle(300,200,350,200);
			iSetColor(255,255,255);
			iText(320 , 360 , PlayerName[min],GLUT_BITMAP_TIMES_ROMAN_24);
			iText(420,360,"has scored a goal!!! ",GLUT_BITMAP_TIMES_ROMAN_24);
			iFilledRectangle(420,250,100,30);
			iText(320,210," Press up arrow to continue",GLUT_BITMAP_HELVETICA_12);
			iSetColor(73,49,210);
			iText(425,255,"Continue",GLUT_BITMAP_TIMES_ROMAN_24);
		}
	}
}
void DirectionChange(void)
{
	if ( ballx + 5 > 579 ) dx = -dx ;
}

void DifferenceCount(void)
{
   DifferX = ballx - playerCoordX[min] ;
   DifferY = bally - playerCoordY[min] ;
   if( DifferX < 0 ) DifferX = DifferX * (-1) ;
   if( DifferY < 0 ) DifferY = DifferY * (-1) ;
   dx = DifferX / DifferY ;
   dy = DifferY / DifferY ;
}

// draws the football field
void drawfield()
{
    iSetColor(255,255,255);
	iFilledRectangle(0,0,1000,700);
	iSetColor(158,192,137);
	iFilledRectangle(10,10,650,680);
	iSetColor(255,255,255);
	iRectangle(80,50,450,600);
	
	iLine(80,350,530,350);
	iCircle(305,350,80);
	iFilledCircle( 305 , 350 , 3 );
	iCircle( 305 , 160 , 50);
	iCircle( 305 , 540 , 50);
	iSetColor( 158 , 192, 137);
    iFilledRectangle(190,50,230,110);
	iFilledRectangle(190,540,230,110);
	iSetColor( 255 , 255 ,255); 
	iRectangle(190,50,230,110);
	iRectangle(190,540,230,110);
	iRectangle(260,50,90,50 );
	iRectangle(260,20,90,30 );
	iRectangle(260,600,90,50);
	iRectangle(260,650,90,30);
	
	iSetColor(229,229,229);
		iFilledRectangle(19,417,30,130);
}


	//function iDraw() is called again and again by the system.
void iDraw()
{
	
	iClear();

	drawfield();
    	iSetColor(243,200,156);
	// drawing for the ball
    	if( distance < 225 )iSetColor(191,238,210);
	
	// showes the player under control
	iFilledCircle(playerCoordX[min] , playerCoordY[min] ,20);
	iSetColor(255,255,255);
	iFilledCircle( (int)ballx ,(int) bally , rad);
	
	// drawing for players
	
	
	BallInTouch() ;
	
    
	//iSetColor(196,255,255);
	//iFilledCircle(oponentCoordX[max],oponentCoordY[max],24);
	// drawing for the goal post
	iSetColor(255,255,255);
	
    iSetColor(255,0,0);
	teamDraw( playerCoordX , playerCoordY);
	iSetColor(0,0,255);
	teamDraw( oponentCoordX ,oponentCoordY);
	iText(700,200,PlayerName[min],GLUT_BITMAP_TIMES_ROMAN_24 );
	
	identiFication();
	
	goalKdraw();
	
	Result();
	
    iSetColor(255,255,255);
	DirectionChange() ;
	iLine(579 ,656, 579 ,42) ;
	if( KickTake == 1) iFilledCircle( (int)ballx , (int)bally , rad );
	netedRectangle ( 260 , 20 , 30 , 90 );
	netedRectangle ( 260 , 650 , 30 , 90 );
}

void netedRectangle( int mx , int my , int differy , int differx )// drawing the two goal post
{
	int x  ;
	for( x = mx+5 ; x < mx + differx ; x = x + 5 )
		iLine( x , my , x , my + differy ) ;
	for( x = my ; x < my + differy ; x = x + 5 )
		iLine( mx , x , mx + differx , x );
}


	//function iMouseMove() is called when the user presses and drags the mouse.
	//(mx, my) is the position where the mouse pointer is.

void iMouseMove(int mx, int my)
{
	//place your codes here
}

void iMouse(int button, int state, int mx, int my)
{
	
	
	if(button == GLUT_LEFT_BUTTON)// && state == GLUT_DOWN)
	{
     	printf("%d , %d ", mx ,my );
	}
	if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
	{
		//place your codes here	
	}
}


	//function iKeyboard() is called whenever the user hits a key in keyboard.
	//key- holds the ASCII value of the key pressed. 

void iKeyboard ( unsigned char key)
{
	// for short pass
    
	if(key=='5')
	{
		DifferenceCount();
	}
	else if( key == '0')
	{
		DifferenceCount();
	}
	
	else  if(key=='8')//movement front
	{
	    moveUp( &playerCoordX[min] , &playerCoordY[min]);
        RunningTeamUp( &playerCoordY[0] , &playerRangeUP[0] , min);
		RunningTeamUp( oponentCoordY ,oponentRangeUp ,max);
		
	}
	
	else if( key=='2')//movement back
	{
	  
		moveDown( &playerCoordX[min] , &playerCoordY[min]);	
		
		RunningTeamDown( &playerCoordY[0] , &playerRangeDOWN[0] , min );
		RunningTeamDown ( oponentCoordY ,oponentRangeDown ,max);
		
	}
     
	else if(key=='4')//movement left
	 {
    	moveLeft( &playerCoordX[min] , &playerCoordY[min]);
		
	 }	
		
	 else if( key=='6')// movement right
	 {
		moveRight( &playerCoordX[min] , &playerCoordY[min] );
		
	 }

	//  Angle movement
    else if( key=='9')
	{
		UpRightMove( &playerCoordX[min] , &playerCoordY[min] ) ;
		
	}
	
	else if( key=='7')
	{
		UpLeftMove( &playerCoordX[min] , &playerCoordY[min] );
		
	}
	
	else if( key=='1')
	{
		DownLeftMove( &playerCoordX[min] , &playerCoordY[min] );
		
	}

	else if( key=='3')
	{
		DownRightMove( &playerCoordX[min] , &playerCoordY[min] );
		
	}

    // Changes player control
	
    
    else if( key == 'i')
    {
	    teamInitialise();
	    
	}
	
	if( key=='e')
	{
      	moveUp( &oponentCoordX[max] , &oponentCoordY[max]);
	}

}

void iSpecialKeyboard(unsigned char key)
{

	if(GLUT_KEY_UP == key)
	{
      f=2;
      teamInitialise();
	}
	//place your codes for other keys here
}
    //function iKeyboard() is called whenever the user hits a key in keyboard.
	//key- holds the ASCII value of the key pressed. 

void iKeyboardUP(unsigned char key)
{
	if(key == '5')
	{
		BallInTouch();
		if( distance < 225 && passTimer == 1 ) index2 = iSetTimer( 5,passPlayer );
	 	else if(distance < 225 && passTimer == 2 ) iResumeTimer(index2);
		
	}
	
	else if(key == '0')
	{	
		BallInTouch();
		KickTake = 1 ;
		if(distance < 225 && timerCount == 1)
		index1 = iSetTimer(2,Kick);
		else if( distance < 225  &&  timerCount == 2 )
			iResumeTimer(index1);
	}
}
void iSpecialKeyboardUP(unsigned char key)
{

	if(GLUT_KEY_UP == key)
	{
	
	}
	//place your codes for other keys here
}

int main()
{
	//place your own initialization codes here.
	min= DistanceCount(playerCoordX ,playerCoordY);
	rand();
	iInitialize(windowX,windowY , "Football 1970");
	return 0;
}	